package com.xiaoy.core.base.menu.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.xiaoy.core.base.menu.dao.MenuDao;
import com.xiaoy.core.base.menu.entity.Menu;
import com.xiaoy.core.base.security.role.entity.Role;
import com.xiaoy.core.base.security.user.service.UserService;
import com.xiaoy.core.base.util.store.config.ConfigTable;
import com.xiaoy.page.PageWrapper;

@Service("menuService")
public class MenuService {

	@Resource(name="menuDao")
	private MenuDao menuDao;
	
	@Resource
	private UserService userService;
	
//	private RoleDao roleDao;
	
	@Transactional
	public void save (Menu entity) {
		menuDao.save(entity);
	}
	
	public List<Menu>getRootMenu () {
		
		Page<Menu> page = new Page<Menu>();
//		page.addFilter(new PropertyFilter("EQS_name","name1"));
		page.addFilter(new PropertyFilter("EQL_parent.id",""));
		menuDao.findPage(page,page.getFilters());
		return page.getResult();
//		return menuDao.getRootMenu();
		
	}
	
	public Page<Menu> filterPage (PageWrapper<Menu> page) {
		
		return menuDao.findPage(page,page.getFilters());
	}
	
	@Transactional
	public void delete  (String id) {
		
		menuDao.delete(Long.valueOf(id));
	}
	
	public List<Menu>getMenuByUser(long username) {
		
		List<Role> roles = userService.getRoleByUserId(username);
		return menuDao.getMenuByRole(roles);
	}
	
	public List<Menu>getRootMenuByUsesr (String usrename) {
		
		return null;
	}
	
}
